package com.springboot.myspringboot.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.springboot.myspringboot.Service.SolidTableService;
import com.springboot.myspringboot.config.Result;
import com.springboot.myspringboot.entity.AreaCount;
import com.springboot.myspringboot.entity.SolidTable;
import com.springboot.myspringboot.entity.User;
import com.springboot.myspringboot.mapper.SolidTableMapper;
import com.springboot.myspringboot.utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.time.LocalDateTime;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/solidlist")
public class SolidTableController {

    @Resource
    SolidTableMapper solidTableMapper;

    @Autowired
    SolidTableService solidTableService;

   /* 根据用户的地区字段筛选全部的数据*/
    @GetMapping("findAll")
    public Result<?> findAll(){
        User user = TokenUtils.getUser();
        try {
//            List<SolidTable> solidAll = solidTableMapper.findAll();
            Integer isAll = user.getIsAll();
            if(isAll.equals(1)){
                List<SolidTable> solidAll = solidTableService.list();
                return Result.success(solidAll);
            }else {
                Integer areaId = user.getAreaId();
                QueryWrapper<SolidTable> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("area_id", areaId);
                List<SolidTable> solidAllByArea = solidTableService.list(queryWrapper);
                return Result.success(solidAllByArea);
            }

        }catch (Exception e){
            return Result.error("500", "服务器错误了");
        }

    }

    @GetMapping("findByArea")
    public Result<?> findByArea(@RequestParam(defaultValue = "1") Integer areaId){
        try {
//            List<SolidTable> solidAll = solidTableMapper.findAll();
            List<SolidTable> solidAll = solidTableService.selectByArea(areaId);
            return Result.success(solidAll);
        }catch (Exception e){
            return Result.error("500", "服务器错误");
        }

    }

    /* 根据用户的地区字段分页查询全部的数据*/
    @GetMapping("findPage")
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "") String search,
                              @RequestParam(required = false) Integer areaId) {
        try {
            User user = TokenUtils.getUser();
            Integer isAll = user.getIsAll();
            QueryWrapper<SolidTable> queryWrapper = new QueryWrapper<>();
            if(!isAll.equals(1)){
                queryWrapper.eq("area_id", user.getAreaId());
            }
//            LambdaQueryWrapper<SolidTable> wrapper = Wrappers.<SolidTable>lambdaQuery();
//            Page<SolidTable> solidPage = solidTableMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
            if(areaId != null){
                queryWrapper.eq("area_id", areaId);
            }
            if(!search.isEmpty()){
                queryWrapper.like("name", search);
            }
            Page<SolidTable> page = new Page<>(pageNum, pageSize);
            Page<SolidTable> solidPage = solidTableService.page(page,queryWrapper);
            return Result.success(solidPage);
        }catch (Exception e){
            return Result.error("500", "服务器错误");
        }
    }

    @PostMapping("save")
    public Result<?> saveSolidTable(@RequestBody SolidTable solidTable){
        try {
            solidTable.setCreateTime(LocalDateTime.now());
            solidTableService.save(solidTable);
//            solidTableMapper.insert(solidTable);
            return Result.success("添加记录成功");
        }catch (Exception e){
            return Result.error("500", "服务器错误");
        }
    }

    @PutMapping("update")
    public Result<?> update(@RequestBody SolidTable solidTable){
        try {
            Boolean isUpdate = solidTableService.updateById(solidTable);
//            Integer num = solidTableMapper.updateById(solidTable);
            if(!isUpdate){
                return Result.error("400", "不存在id");
            }
            return Result.success("更改成功");
        } catch (Exception e){
            return Result.error("500", "服务器错误");
        }
    }

    @DeleteMapping("delete")
    public Result<?> delete(@RequestParam Integer id){

        try {
            Boolean isRemoved = solidTableService.removeById(id);
//            Integer deleteId = solidTableMapper.deleteById(id);
            if(!isRemoved){
                return Result.error("500", "不存在id");
            }
            return Result.success("删除成功");
        } catch (Exception e){
            return Result.error("500", "服务器错误");
        }
    }

    @GetMapping("findAreaCount")
    public Result<?> findAreaCount(){
        try {
            List<AreaCount> list = solidTableService.findAreaCount();
            return Result.success(list);
        }catch (Exception e){
            return Result.error("500", "服务器错误");
        }
    }

}
